package edu.zhku.ttms.view.panel.content;

import edu.zhku.ttms.service.UserService;
import edu.zhku.ttms.service.impl.UserServiceImpl;
import edu.zhku.ttms.utils.CheckUtil;
import edu.zhku.ttms.view.ViewController;
import edu.zhku.ttms.view.font.LeftLabelFont;

import javax.swing.*;
import java.awt.*;

/**
 * @author Feng
 * @since 2021/4/23 22:02
 */
public class RegisterPanel extends ViewPanel {

    UserService userService = new UserServiceImpl();

    public RegisterPanel() {

        super(false);

        Font font = new LeftLabelFont();

        JRadioButton driverRadio = new JRadioButton("我是驾驶员", true);
        driverRadio.setFont(font);
        driverRadio.setBounds(260, 105, 220, 30);
        this.add(driverRadio);

        JRadioButton policeRadio = new JRadioButton("我是交警", false);
        policeRadio.setFont(font);
        policeRadio.setBounds(480, 105, 220, 30);
        this.add(policeRadio);

        ButtonGroup buttonGroup = new ButtonGroup();
        buttonGroup.add(driverRadio);
        buttonGroup.add(policeRadio);

        JLabel loginNameLabel = new JLabel("用户名");
        loginNameLabel.setFont(font);
        loginNameLabel.setBounds(260, 160, 132, 30);
        this.add(loginNameLabel);

        JTextField loginNameInput = new JTextField();
        loginNameInput.setFont(font);
        loginNameInput.setToolTipText("若为驾驶员请填写驾驶证号，若为交警请填写交警代号");
        loginNameInput.setBounds(392, 160, 308, 30);
        this.add(loginNameInput);

        JLabel passwordLabel1 = new JLabel("密码");
        passwordLabel1.setFont(font);
        passwordLabel1.setBounds(260, 215, 132, 30);
        this.add(passwordLabel1);

        JPasswordField passwordInput1 = new JPasswordField();
        passwordInput1.setBounds(392, 215, 308, 30);
        this.add(passwordInput1);

        JLabel passwordLabel2 = new JLabel("确认密码");
        passwordLabel2.setFont(font);
        passwordLabel2.setBounds(260, 270, 132, 30);
        this.add(passwordLabel2);

        JPasswordField passwordInput2 = new JPasswordField();
        passwordInput2.setBounds(392, 270, 308, 30);
        this.add(passwordInput2);

        JLabel tip = new JLabel();
        tip.setForeground(Color.RED);
        tip.setFont(font);
        tip.setBounds(392, 325, 440, 30);
        this.add(tip);

        JButton goBackButton = new JButton("返回");
        goBackButton.addActionListener(e -> ViewController.changeView(new LoginPanel()));
        goBackButton.setBounds(392, 380, 148, 48);
        this.add(goBackButton);

        JButton registerButton = new JButton("注册");
        registerButton.addActionListener(e -> {
            String loginName = loginNameInput.getText();
            String password = passwordInput1.getText();
            String rePassword = passwordInput2.getText();
            Integer roleId = null;
            if (driverRadio.isSelected()) {
                roleId = 3;
            } else if (policeRadio.isSelected()) {
                roleId = 2;
            }
            String registerRes = userService.register(loginName, password, rePassword, roleId);
            if (CheckUtil.isSuccess(registerRes)) {
                tip.setText("");
                JOptionPane.showMessageDialog(this, "注册成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                ViewController.changeView(new LoginPanel());
            } else {
                tip.setText(registerRes);
            }
        });
        registerButton.setBounds(546, 380, 148, 48);
        this.add(registerButton);
    }
}
